package com.six666.medicaldevicetestingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six666.medicaldevicetestingsystem.dto.Equipment_Scheme_DTO;
import com.six666.medicaldevicetestingsystem.entity.EquipmentScheme;
import com.six666.medicaldevicetestingsystem.mapper.EquipmentScheme_Mapper;
import com.six666.medicaldevicetestingsystem.service.Equipment_Scheme_Service;
import com.six666.medicaldevicetestingsystem.service.ProjectFile_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Equipment_Scheme_Service_Impl extends ServiceImpl<EquipmentScheme_Mapper, EquipmentScheme> implements Equipment_Scheme_Service{
    @Autowired
    ProjectFile_Service project_file_service;
    @Override
    public Equipment_Scheme_DTO get_equipment_scheme_by_id(int id) {
        QueryWrapper<EquipmentScheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schemeid",id);
        EquipmentScheme scheme=getOne(queryWrapper);
        String pic=project_file_service.get_url_by_id(scheme.getPicture());
        String detail=project_file_service.get_url_by_id(scheme.getSchemedetail());
        Equipment_Scheme_DTO res=new Equipment_Scheme_DTO();
        res.setEquipment_id(scheme.getEquipmentid());
        res.setScheme_id(scheme.getSchemeid());
        res.setPicture_url(pic);
        res.setScheme_number(scheme.getSchemenumber());
        res.setScheme_detail(detail);
        res.setSource(scheme.getSource());
        res.setScheme_name(scheme.getSchemename());
        return res;
    }

    @Override
    public boolean add_or_update_pic(int scheme_id, int pic_id) {
        QueryWrapper<EquipmentScheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schemeid",scheme_id);
        EquipmentScheme scheme=getOne(queryWrapper);
        scheme.setPicture(pic_id);
        QueryWrapper<EquipmentScheme> whereWrapper = new QueryWrapper<>();
        whereWrapper.eq("schemeid", scheme_id);
        return update(scheme, whereWrapper);
    }

    @Override
    public boolean add_or_update_pdf(int scheme_id, int pdf_id) {
        QueryWrapper<EquipmentScheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schemeid",scheme_id);
        EquipmentScheme scheme=getOne(queryWrapper);
        scheme.setSchemedetail(pdf_id);
        QueryWrapper<EquipmentScheme> whereWrapper = new QueryWrapper<>();
        whereWrapper.eq("schemeid", scheme_id);
        return update(scheme, whereWrapper);
    }

    @Override
    public int get_equip_id_by_scheme_id(int scheme_id) {
        QueryWrapper<EquipmentScheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schemeid", scheme_id);
        EquipmentScheme scheme=getOne(queryWrapper);
        return scheme.getEquipmentid();
    }

    @Override
    public boolean add_scheme(int equipment_id, String scheme_name, String source) {
        EquipmentScheme scheme=new EquipmentScheme();
        scheme.setSchemename(scheme_name);
        scheme.setEquipmentid(equipment_id);
        scheme.setSchemenumber(source);
        return save(scheme);
    }

    @Override
    public EquipmentScheme get_by_id(int id) {
        QueryWrapper<EquipmentScheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schemeid", id);
        return getOne(queryWrapper);
    }
}
